package one.three.seven;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class Stack<Item> {

	private Node<Item> first;
	private int n;

	public Stack() {
		first = null;
		n = 0;
	}

	public boolean isEmpty() {
		return first == null;
	}

	public int size() {
		return n;
	}

	public void push(Item item) {
		Node oldfirst = first;
		first = new Node();
		first.item = item;
		first.next = oldfirst;
		n++;
	}

	public Item pop() {
		if (isEmpty()) {
			throw new NoSuchElementException("Stack underflow");
		}
		Item item = (Item) first.item;
		first = first.next;
		n--;
		return item;
	}

	public Item seek() {
		if (!isEmpty())
			return first.item;
		return null;
	}

	private class Node<Item> {
		Item item;
		Node<Item> next;
	}

	private class LinkedIterator<Item> implements Iterator<Item> {
		private Node<Item> current;

		public LinkedIterator(Node<Item> first) {
			this.current = first;
		}

		@Override
		public boolean hasNext() {

			return current != null;
		}

		@Override
		public void remove() {
			throw new UnsupportedOperationException();
		}

		@Override
		public Item next() {
			if (!hasNext()) {
				throw new NoSuchElementException();
			}
			Item item = current.item;
			current = current.next;
			return item;
		}

	}

	public static void main(String[] args) {
		Stack<String> s = new Stack<String>();
		s.push("a");
		s.push("b");
		s.push("c");

		String str1 = s.seek();
		String str2 = s.pop();
		System.out.println(str1.equals(str2));
		

	}
}